'''
Main program for GUI and calling
Created on Jul 19, 2014
@author: ANAND
'''
import os.path
from Tkinter import *
import ttk
import myqueries

class App(Frame):
    
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.master = master
        #Fill data on startup
        self.filecheck()
        self.menu_bar()
        self.tool_bar()
        self.buytable()
        self.selltable()
        self.alltable()
        
    def filecheck(self):
        if os.path.isfile("eb2day.db"):
            self.my_fill = 1
        else:
            self.my_fill = 0
    
    def hello(self):
        print "Hello!"
    
    def menu_bar(self):
        self.menubar = Menu(self)
        
        self.filemenu = Menu(self.menubar, tearoff=0)
        self.menubar.add_cascade(label='File', menu=self.filemenu)
        self.filemenu.add_command(label="Open", command=self.hello)
        self.filemenu.add_command(label='Save', command=self.hello)
        self.filemenu.add_command(label='Exit', command=self.quit)
        
        self.helpmenu = Menu(self.menubar, tearoff=0)
        self.menubar.add_cascade(label='Help', menu=self.helpmenu)
        self.helpmenu.add_command(label='About', command=self.hello)
        
        self.master.config(menu=self.menubar)
    
    def get_dates(self):
        self.pre_date = self.pre_date_v.get()
        self.cur_date = self.curr_date_v.get()
        print "D1: ", self.pre_date
        print "D0: ", self.cur_date
        
        import processor
        processor.processit(self.cur_date, self.pre_date)
        
        self.my_fill = 1
        self.buytable()
        self.selltable()
        self.alltable()
    
    def tool_bar(self):
        self.dateformat = Label(self, text='Enter Dates in (dd-mm-yyyy) format')
        self.dateformat.grid(row=0, column=0, padx=15, pady=10, sticky=W)
        
        self.infopredate = Label(self, text='Previous day date:')
        self.infopredate.grid(row=0, column=1, padx=2)
        
        self.pre_date_v = StringVar()
        self.enterpredate = Entry(self, textvariable=self.pre_date_v)
        self.enterpredate.grid(row=0, column=2, sticky=W)
        
        self.infopredate = Label(self, text='Current day date:')
        self.infopredate.grid(row=0, column=5, padx=2)
        
        self.curr_date_v = StringVar()
        self.entercurrdate = Entry(self, textvariable=self.curr_date_v)
        self.entercurrdate.grid(row=0, column=6, sticky=W)
        
        self.getdates = Button(self, text="Get Data", width=10, command=self.get_dates)
        self.getdates.grid(row=0, column=7, padx=15)
        
    def buytable(self):
        self.buylabel = Label(self, text="Buy Stocks")
        self.buylabel.grid(row=1, column=0, padx=15, sticky=W)
        
        self.tree = ttk.Treeview(self, show="headings")
        ysb = ttk.Scrollbar(self, orient='vertical', command=self.tree.yview)
        self.tree.configure(yscroll=ysb.set)
        self.tree["columns"] = ("symbol", "cclose", "pclose", "percent", "volume", "strsign", "olh")
        self.tree.column("symbol", width=100)
        self.tree.column("cclose", width=90)
        self.tree.column("pclose", width=80)
        self.tree.column("percent", width=60)
        self.tree.column("volume", width=80)
        self.tree.column("strsign", width=80)
        self.tree.column("olh", width=80)
        self.tree.heading("symbol", text="Company")
        self.tree.heading("cclose", text="Current Price")
        self.tree.heading("pclose", text="Prv. Close")
        self.tree.heading("percent", text="% change")
        self.tree.heading("volume", text="Volume")
        self.tree.heading("strsign", text="Strong Buy")
        self.tree.heading("olh", text="Open=Low")
        
        if self.my_fill == 1:
            buyresult = myqueries.allbuy()
            for buyres in buyresult:
                self.tree.insert("", 0, text="Line 1", values=buyres)
        
        #self.tree.insert("", 0, text="Line 1", values=("1A", "1B", "1C", "1D"))
        self.tree.grid(row=2, column= 0, columnspan=7, padx=15, sticky=W)
        ysb.grid(row=2, column=5, sticky='ns')
        
    def selltable(self):
        self.selllabel = Label(self, text="Sell Stocks")
        self.selllabel.grid(row=1, column=6, padx=15, sticky=W)
        self.tree = ttk.Treeview(self, show="headings")
        ysb = ttk.Scrollbar(self, orient='vertical', command=self.tree.yview)
        self.tree.configure(yscroll=ysb.set)
        self.tree["columns"] = ("symbol", "cclose", "pclose", "percent", "volume", "strsign", "olh")
        self.tree.column("symbol", width=100)
        self.tree.column("cclose", width=90)
        self.tree.column("pclose", width=80)
        self.tree.column("percent", width=60)
        self.tree.column("volume", width=80)
        self.tree.column("strsign", width=80)
        self.tree.column("olh", width=80)
        self.tree.heading("symbol", text="Company")
        self.tree.heading("cclose", text="Current Price")
        self.tree.heading("pclose", text="Prv. Close")
        self.tree.heading("percent", text="% change")
        self.tree.heading("volume", text="Volume")
        self.tree.heading("strsign", text="Strong Sell")
        self.tree.heading("olh", text="Open=High")
        
        if self.my_fill == 1:
            sellresult = myqueries.allsell()
            for sellres in sellresult:
                self.tree.insert("", 0, text="Line 1", values=sellres)
        
        self.tree.grid(row=2, column=6, columnspan=13, padx=15, sticky=W)
        ysb.grid(row=2, column=16, sticky='ns')
        
    def alltable(self):
        self.alllabel = Label(self, text="All Stocks")
        self.alllabel.grid(row=3, column=0, sticky=W, padx=15)
        
        self.tree = ttk.Treeview(self, show="headings")
        ysb = ttk.Scrollbar(self, orient='vertical', command=self.tree.yview)
        self.tree.configure(yscroll=ysb.set)
        self.tree["columns"] = ("symbol", "series", "cclose", "change", "percent", "open", "high", "low", "pivote", "volume", 
                                "phigh", "plow", "pclose", "eodsign", "strsign", "olh")
        self.tree.column("symbol", width=100)
        self.tree.column("series", width=70)
        self.tree.column("cclose", width=80)
        self.tree.column("change", width=70)
        self.tree.column("percent", width=80)
        self.tree.column("open", width=70)
        self.tree.column("high", width=70)
        self.tree.column("low", width=70)
        self.tree.column("pivote", width=70)
        self.tree.column("volume", width=80)
        self.tree.column("phigh", width=70)
        self.tree.column("plow", width=70)
        self.tree.column("pclose", width=70)
        self.tree.column("eodsign", width=80)
        self.tree.column("strsign", width=80)
        self.tree.column("olh", width=80)
        
        self.tree.heading("symbol", text="Symbol")
        self.tree.heading("series", text="Series")
        self.tree.heading("cclose", text="Price")
        self.tree.heading("change", text="Change")
        self.tree.heading("percent", text="% Change")
        self.tree.heading("open", text="Open")
        self.tree.heading("high", text="High")
        self.tree.heading("low", text="Low")
        self.tree.heading("pivote", text="Pivote")
        self.tree.heading("volume", text="Volume")
        self.tree.heading("phigh", text="Prv. High")
        self.tree.heading("plow", text="Prv. Low")
        self.tree.heading("pclose", text="Prv. Close")
        self.tree.heading("eodsign", text="Buy / Sell")
        self.tree.heading("strsign", text="Is Strong")
        self.tree.heading("olh", text="O-L-H sign")
        
        if self.my_fill == 1:
            allresult = myqueries.allstocks()
            for allres in allresult:
                self.tree.insert("", 0, text="Line 1", values=allres)
        
        self.tree.grid(row=4, column=0, columnspan=16, padx=15, sticky=W)
        ysb.grid(row=4, column=16, sticky='ns')

if __name__ == "__main__":
    root = Tk()
    root.title("StockPro EOD")
    root.geometry("1280x600")
    app = App(root)
    app.mainloop()
